package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.VstarCustomerSatisfactionReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarCustomerSatisfactionReport.VstarCustomerSatisfactionReport;
import com.ugoodtech.mdcc.core.domain.VstarCustomerSatisfactionReport.VstarCustomerSatisfactionReport_Brand;
import com.ugoodtech.mdcc.core.domain.VstarServiceReport.VstarServiceReport_Target;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.impl.VstarCustomerServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class VstarCustomerSatisfactionReportServiceImpl implements VstarCustomerSatisfactionReportService {

    private static final Logger logger = LoggerFactory.getLogger(VstarCustomerSatisfactionReportServiceImpl.class);

    @Autowired
    private StationCustomerReportRepository stationCustomerReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private CustomerRegularReportRepository customerRegularReportRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private VstarCustomerServiceImpl vstarCustomerService;

    private int countPerson = 0;


    ////////////////////////改////////////////////////////////

    @Override
    public JsonResponse statReport(String provinces, String brand, String category, Long stationId,
                                   Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, Long serviceType,
                                   Integer serviceNum, Long userId, Boolean iFillin, String scoreType, String followType, Long loginUserId,
                                   Long departmentId) {
        if (serviceNum != null && serviceNum != 0) {
            if (serviceNum >= Long.MAX_VALUE) {
                return JsonResponse.errorResponseWithError("抱歉", "服务次数太多");
            }
        }
        List<CustomerRegularReport> customerRegularReportList = vstarCustomerService.
                builder(provinces, brand, category, stationId, serviceStartTime, serviceEndTime, startTime, endTime, serviceType,
                        serviceNum, userId, iFillin, scoreType, followType, loginUserId, departmentId);
        List<StationReport> reportList = toStationReport(customerRegularReportList);
        countPerson = reportList.size();
        logger.info("查出的满意度报告有：" + countPerson + "份");
        logger.info("最终查出的满意度报告有：" + reportList.size() + "份");
        VstarCustomerSatisfactionReport statCustomerReport = new VstarCustomerSatisfactionReport();
        statCustomerReport.setCountPerson(countPerson);
        if (StringUtils.isNotEmpty(brand) && StringUtils.isEmpty(category)) {
            Integer IBS = 0;
            Integer BPC = 0;
            Integer FBS = 0;
            Integer DEALERS = 0;
            String[] brandArray = brand.split(",");
            Map<String, Integer> countBrandMap = new HashMap<>();
            List<String[]> brandStr = new ArrayList<>();
            List<String[]> otherBrandArray = new ArrayList<>();
            Map<String, Integer> otherCountBrandMap = new HashMap<>();
            for (StationReport report : reportList) {
                for (String brandName : brandArray) {
                    if (StringUtils.isNotEmpty(brandName)) {
                        if (report.getStation().getCategory().equals(Constants.FOR_S) && report.getStation().getBrand().indexOf(brandName) != -1) {
                            if (countBrandMap.containsKey(brandName)) {
                                Integer value = countBrandMap.get(brandName);
                                countBrandMap.put(brandName, value + 1);
                            } else {
                                countBrandMap.put(brandName, 1);
                            }
                        }
                    }
                }
                if (report.getStation().getCategory().equals(Constants.IBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = countBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    IBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.BPC)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = countBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    BPC += 1;
                }
                if (report.getStation().getCategory().equals(Constants.FBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = countBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    FBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = countBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    DEALERS += 1;
                }
            }
            String[] str = new String[2];
            for (Map.Entry<String, Integer> entry : countBrandMap.entrySet()) {
                str = new String[2];
                str[0] = entry.getKey();
                str[1] = entry.getValue().toString();
                brandStr.add(str);
            }
            if (IBS != 0) {
                str = new String[2];
                str[0] = Constants.IBS;
                str[1] = String.valueOf(IBS);
                brandStr.add(str);
            }
            if (BPC != 0) {
                str = new String[2];
                str[0] = Constants.BPC;
                str[1] = String.valueOf(BPC);
                brandStr.add(str);
            }
            if (FBS != 0) {
                str = new String[2];
                str[0] = Constants.FBS;
                str[1] = String.valueOf(FBS);
                brandStr.add(str);
            }
            if (DEALERS != 0) {
                str = new String[2];
                str[0] = Constants.DEALERS;
                str[1] = String.valueOf(DEALERS);
                brandStr.add(str);
            }
            for (Map.Entry<String, Integer> entry : otherCountBrandMap.entrySet()) {
                String[] otherBrand = new String[2];
                otherBrand[0] = entry.getKey();
                otherBrand[1] = entry.getValue().toString();
                otherBrandArray.add(otherBrand);
            }
            VstarCustomerSatisfactionReport_Brand countBrand = new VstarCustomerSatisfactionReport_Brand();
            countBrand.setBrandArray(brandStr);
            countBrand.setOtherBrandList(otherBrandArray);
            countBrand.setCountPerson(countPerson);
            countBrand.setOtherBrand("");
            statCustomerReport.setVstarCustomerSatisfactionReport_brand(countBrand);
        } else {
            if (StringUtils.isNotEmpty(brand)) {
                String[] brandArray = brand.split(",");
                statCustomerReport.setVstarCustomerSatisfactionReport_brand(getBrand_new(brandArray, reportList));
            } else {
                statCustomerReport.setVstarCustomerSatisfactionReport_brand(getBrand_new(null, reportList));
            }
        }
        statCustomerReport.setVstarServiceReport_Target(getTarget(reportList));
        return JsonResponse.successResponseWithData(statCustomerReport);
    }


    private VstarCustomerSatisfactionReport_Brand getBrand_new(String[] brandArray, List<StationReport> reportList) {
        List<String> brandNames = new ArrayList<>();
        for (StationReport report : reportList) {
            if (report.getStation().getCategory().equals(Constants.FOR_S)) {
                String[] brandName = report.getStation().getBrand().split(",");
                for (String brand : brandName) {
                    if (brandArray != null) {
                        for (String str : brandArray) {
                            if (!brand.equals(str)) {
                                continue;
                            } else {
                                if (StringUtils.isNotEmpty(brand)) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    } else {
                        if (StringUtils.isNotEmpty(brand)) {
                            brandNames.add(brand);
                        }
                    }
                }
            } else {
                if (report.getStation().getCategory().equals(Constants.IBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                        if (answerOption != null) {
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                if (answerOption != null) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.IBS);
                }
                if (report.getStation().getCategory().equals(Constants.BPC)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                        if (answerOption != null) {
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                if (answerOption != null) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.BPC);
                }
                if (report.getStation().getCategory().equals(Constants.FBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                        if (answerOption != null) {
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                if (answerOption != null) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.FBS);
                }
                if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                        if (answerOption != null) {
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(brand);
                                if (answerOption != null) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.DEALERS);
                }
            }
        }
        return countBrand(brandNames);
    }

    private VstarCustomerSatisfactionReport_Brand countBrand(List<String> brandNames) {
        Map<String, Integer> brandMap = new HashMap<>();
        for (String brand : brandNames) {
            if (brandMap.containsKey(brand)) {
                Integer value = brandMap.get(brand);
                brandMap.put(brand, value + 1);
            } else {
                brandMap.put(brand, 1);
            }
        }
        VstarCustomerSatisfactionReport_Brand brand = new VstarCustomerSatisfactionReport_Brand();
        List<String[]> brandList = new ArrayList<>();
        List<String[]> otherBrnadList = new ArrayList<>();
        Map<String, Integer> otherbrandMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : brandMap.entrySet()) {
            if (entry.getKey().equals(Constants.IBS) || entry.getKey().equals(Constants.FBS) || entry.getKey().equals(Constants.BPC)) {
                continue;
            }
            String[] brandArray = new String[2];
            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(entry.getKey());
            if (answerOption != null) {
                brandArray[0] = entry.getKey();
                brandArray[1] = entry.getValue().toString();
                brandList.add(brandArray);
            } else {
                if (otherbrandMap.containsKey("其他品牌")) {
                    Integer value = otherbrandMap.get("其他品牌");
                    otherbrandMap.put("其他品牌", value + entry.getValue());
                } else {
                    otherbrandMap.put("其他品牌", entry.getValue());
                }
                String[] otherBrandArray = new String[2];
                otherBrandArray[0] = entry.getKey();
                otherBrandArray[1] = entry.getValue() + "";
                otherBrnadList.add(otherBrandArray);
            }
        }
        for (Map.Entry<String, Integer> entry : brandMap.entrySet()) {
            if (entry.getKey().equals(Constants.IBS) || entry.getKey().equals(Constants.FBS) || entry.getKey().equals(Constants.BPC)) {
                String[] brandArray = new String[2];
                brandArray[0] = entry.getKey();
                brandArray[1] = entry.getValue().toString();
                brandList.add(brandArray);
            }
        }
        if (otherbrandMap != null && !otherbrandMap.isEmpty()) {
            String[] otherBrand = new String[2];
            otherBrand[0] = "其他品牌";
            otherBrand[1] = otherbrandMap.get("其他品牌").toString();
            brandList.add(otherBrand);
        }
        if (otherBrnadList == null || otherBrnadList.size() <= 0) {
            brand.setOtherBrandList(otherBrnadList);
        }
        brand.setOtherBrandList(otherBrnadList);
        brand.setBrandArray(brandList);
        brand.setCountPerson(countPerson);
        return brand;
    }

    //统计服务目标
    private VstarServiceReport_Target getTarget(List<StationReport> reports) {
        List<String> targetIdList = new ArrayList<>();
        List<StationReport> reportList = new ArrayList<>();
        for (StationReport report : reports) {
            String[] targetIds = report.getServiceTypAnswerIds().split(",");
            for (String target : targetIds) {
                if (StringUtils.isNotEmpty(target)) {
                    targetIdList.add(target);
                    if (target.equals("191")) {
                        reportList.add(report);
                    }
                }
            }
        }
        VstarServiceReport_Target targetList = countTarget(targetIdList);
        targetList.setOtherTarget(targetOther(reportList));
        targetList.setCountPerson(countPerson);
        return targetList;
    }

    private VstarServiceReport_Target countTarget(List<String> targetIdList) {
        Map<Long, Integer> targetMap = new HashMap<>();
        for (String target : targetIdList) {
            if (targetMap.containsKey(Long.valueOf(target))) {
                Integer value = targetMap.get(Long.valueOf(target));
                targetMap.put(Long.valueOf(target), value + 1);
            } else {
                targetMap.put(Long.valueOf(target), 1);
            }
        }
        VstarServiceReport_Target target = new VstarServiceReport_Target();
        List<String[]> targetList = new ArrayList<>();
        for (Map.Entry<Long, Integer> entry : targetMap.entrySet()) {
            String[] targetArray = new String[2];
            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(entry.getKey());
            if (answerOption != null) {
                targetArray[0] = answerOption.getName();
                targetArray[1] = entry.getValue().toString();
                targetList.add(targetArray);
            }
        }
        target.setTarget(targetList);
        target.setCountPerson(countPerson);
        return target;
    }

    private List<String[]> targetOther(List<StationReport> reportList) {
        List<String[]> otherTargerList = new ArrayList<>();
        for (StationReport report : reportList) {
//            if (report.getServiceTypAnswerIds().indexOf("191") == -1) {
//                continue;
//            }
            StationRegularServiceReport serviceReport = stationRegularServiceReportRepository.selectAnswer(report.getId(), 191L);
            if (serviceReport == null) {
                continue;
            }
            String realName = "";
            if (report.getUser() != null) {
                UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
                if (userProfile != null) {
                    if (StringUtils.isNotEmpty(userProfile.getRealName())) {
                        realName = userProfile.getRealName();
                    }
                }
            }
            String[] otherTarger = new String[5];
            otherTarger[0] = report.getStation().getName();
            otherTarger[1] = report.getStation().getStationNum();
            otherTarger[2] = report.getCreationTimeStr();
            otherTarger[3] = serviceReport.getAnswerOther();
            otherTarger[4] = realName;
            otherTargerList.add(otherTarger);
        }
        return otherTargerList;
    }

    //筛选条件
    private List<CustomerRegularReport> builder(String provinces, String brand, String category, Long stationId,
                                                Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, Long serviceType,
                                                Integer serviceNum, Long userId, Boolean iFillin, String scoreType, String followType, Long loginUserId) {
        User user = userRepository.findOne(loginUserId);
        UserProfile userProfile = userProfileRepository.findByUserId(loginUserId);
        if (userProfile == null) {
            return null;
        }
        QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qCustomerRegularReport.deleted.eq(false));
        builder.and(qCustomerRegularReport.enabled.eq(true));
        builder.and(qCustomerRegularReport.customerReport.isNotNull());

        if (StringUtils.isNotEmpty(category)) {
            builder.and(qCustomerRegularReport.regularReport.station.category.eq(category));
        }
        if (stationId != null && stationId != 0) {
            builder.and(qCustomerRegularReport.regularReport.station.id.eq(stationId));
        }
//        if (StringUtils.isNotEmpty(name)) {
//            builder.and(qCustomerRegularReport.regularReport.station.name.like("%" + name + "%"));
//        }
        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qCustomerRegularReport.regularReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qCustomerRegularReport.regularReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qCustomerRegularReport.creationTime.after(new Date(startTime)));
            builder.and(qCustomerRegularReport.creationTime.before(new Date(endTime)));
        }
        if (serviceType != null && serviceType != 0) {
            builder.and(qCustomerRegularReport.regularReport.serviceTypAnswerIds.like("%" + serviceType + "%"));
        }
        if (serviceNum != null && serviceNum != 0) {
            builder.and(qCustomerRegularReport.regularReport.user.serviceNum.eq(serviceNum));
        }
        if (iFillin != null) {
            if (iFillin.equals(false)) {
                builder.and(qCustomerRegularReport.type.eq(false));
            } else {
                builder.and(qCustomerRegularReport.type.eq(true));
            }
        }
        String[] str = new String[61];
        for (int i = 1; i < str.length; i++) {
            str[i] = i + "";
        }
        str[0] = "0";
        String[] notStr = new String[39];
        int index = 61;
        for (int i = 0; i < 39; i++) {
            notStr[i] = index + "";
            index += 1;
        }
        if (StringUtils.isNotEmpty(scoreType)) {
            if (scoreType.equals(Constants.FULL_MARKS)) {
                builder.and(qCustomerRegularReport.score.eq("100"));
            }
            if (scoreType.equals(Constants.MORE_THAN_SIXTY)) {
                builder.and(qCustomerRegularReport.score.in(notStr));
            }
//            if (scoreType.equals(Constants.BELOW_THAN_SIXTY)) {
//                builder.and(qCustomerRegularReport.score.in(str));
//            }
        }

        if (userId != null && userId != 0) {
            builder.and(qCustomerRegularReport.regularReport.user.id.eq(userId));
        }


        List<CustomerRegularReport> customerRegularReportList = (List<CustomerRegularReport>) customerRegularReportRepository.findAll(builder);
        List<CustomerRegularReport> reportList = new ArrayList<>();
        for (CustomerRegularReport report : customerRegularReportList) {
            if (StringUtils.isNotEmpty(scoreType) && scoreType.equals(Constants.BELOW_THAN_SIXTY)) {
                QStationCustomerReport qStationCustomerReport = QStationCustomerReport.stationCustomerReport;
                builder = new BooleanBuilder();
                builder.and(qStationCustomerReport.deleted.eq(false));
                builder.and(qStationCustomerReport.enabled.eq(true));
                builder.and(qStationCustomerReport.stationReport.id.eq(report.getCustomerReport().getId()));
                List<StationCustomerReport> customerReportList = (List<StationCustomerReport>) stationCustomerReportRepository.findAll();
                for (StationCustomerReport r : customerReportList) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(r.getAnswerId()));
                    if (answerOption.getName().equals("80%") || answerOption.getName().equals("100%")) {
                        continue;
                    }
                }
            }
            reportList.add(report);
        }
        logger.info("最终查出的报告有：" + customerRegularReportList.size() + "份");
        return reportList;
    }

    /**
     * 封裝成StationReport
     *
     * @param customerRegularReportList
     * @return
     */
    private List<StationReport> toStationReport(List<CustomerRegularReport> customerRegularReportList) {
        List<StationReport> stationReports = new ArrayList<>();
        for (CustomerRegularReport report : customerRegularReportList) {
            if (report.getCustomerReport() != null) {
                stationReports.add(report.getRegularReport());
            }

        }
        return stationReports;
    }

    @Override
    public JsonResponse getScore(String province, String brand, String category, String stationName,
                                 Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, String serviceType,
                                 Integer serviceNum, Long userId, String userName, String department, String city, String customerStat,
                                 String customerType,String paintBrand,String productPosition,String reportStatus,String score,String isManaged,
                                 String serviceReportStatus,Boolean paintType,String payService,String sentTime,String serviceTime,String stationNum) {
        List<CustomerRegularReport> reportList = vstarCustomerService.builder(province, brand, category, stationName, serviceStartTime,
                serviceEndTime, startTime, endTime, serviceType, serviceNum, userId, userName, department, city, customerStat, customerType, paintBrand,
                productPosition, reportStatus, score, isManaged, serviceReportStatus, paintType, payService, sentTime, serviceTime, stationNum);
        List<String[]> scoreList = getScore(reportList);
        return JsonResponse.successResponseWithData(scoreList);
    }

    private String[] questionId_159 = new String[12];

    private String[] questionId_160 = new String[12];

    private String[] questionId_161 = new String[12];

    private String[] questionId_162 = new String[12];

    private String[] questionId_163 = new String[12];

    private String[] questionId_164 = new String[12];

    private String[] questionId_166 = new String[12];

    //[0~12] [题目名称:0,100分:1,百分比:2,80分:3,百分比:4,60分:5,百分比:6,40分:7,百分比:8,20分:9,百分比:10,平均分:11]
    private String[] initValue(String[] arrayStr) {
        for (int i = 0; i < arrayStr.length; i++) {
            arrayStr[i] = "0";
        }
        return arrayStr;
    }

    private List<String[]> getScore(List<CustomerRegularReport> customerRegularReportList) {
        questionId_159 = initValue(questionId_159);
        questionId_160 = initValue(questionId_160);
        questionId_161 = initValue(questionId_161);
        questionId_162 = initValue(questionId_162);
        questionId_163 = initValue(questionId_163);
        questionId_164 = initValue(questionId_164);
        questionId_166 = initValue(questionId_166);

        questionId_159[0] = "提供这次服务前, 技术服务人员与您的沟通如何？";
        questionId_160[0] = "技术服务人员是否准时抵达现场？";
        questionId_161[0] = "对服务人员形象满意度？(如是否穿上公司制服)";
        questionId_162[0] = "您对这次技术服务人员的工作态度满意度如何？";
        questionId_163[0] = "这次技术服务支持是否达到您的期望？";
        questionId_164[0] = "您对这次技术服务工作整体满意度评分如何？";
        questionId_166[0] = "您对AkzoNobel技术服务工作整体满意度评分如何？";


        List<String[]> scoreList = new ArrayList<>();
        List<StationReport> stationReportList = new ArrayList<>();
        for (CustomerRegularReport customerRegularReport : customerRegularReportList) {
            if (customerRegularReport.getCustomerReport() != null) {
                stationReportList.add(customerRegularReport.getCustomerReport());
            }
        }
        for (StationReport stationReport : stationReportList) {
            BooleanBuilder builder = new BooleanBuilder();
            QStationCustomerReport qStationCustomerReport = QStationCustomerReport.stationCustomerReport;
            builder.and(qStationCustomerReport.deleted.eq(false));
            builder.and(qStationCustomerReport.enabled.eq(true));
            builder.and(qStationCustomerReport.stationReport.id.eq(stationReport.getId()));
            List<StationCustomerReport> stationCustomerReports = (List<StationCustomerReport>) stationCustomerReportRepository.findAll(builder);
            for (StationCustomerReport customerReport : stationCustomerReports) {
                //[0~12] [题目名称:0,100分:1,百分比:2,80分:3,百分比:4,60分:5,百分比:6,40分:7,百分比:8,20分:9,百分比:10,平均分:11]
                //提供这次服务前, 技术服务人员与您的沟通如何？
                if (customerReport.getQuestionId().equals(159L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("192")) {
                        Integer value = Integer.valueOf(questionId_159[9]);
                        questionId_159[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("198")) {
                        Integer value = Integer.valueOf(questionId_159[7]);
                        questionId_159[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("204")) {
                        Integer value = Integer.valueOf(questionId_159[5]);
                        questionId_159[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("210")) {
                        Integer value = Integer.valueOf(questionId_159[3]);
                        questionId_159[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("216")) {
                        Integer value = Integer.valueOf(questionId_159[1]);
                        questionId_159[1] = String.valueOf(value + 1);
                    }
                }
                //技术服务人员是否准时抵达现场？
                if (customerReport.getQuestionId().equals(160L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("193")) {
                        Integer value = Integer.valueOf(questionId_160[9]);
                        questionId_160[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("199")) {
                        Integer value = Integer.valueOf(questionId_160[7]);
                        questionId_160[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("205")) {
                        Integer value = Integer.valueOf(questionId_160[5]);
                        questionId_160[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("211")) {
                        Integer value = Integer.valueOf(questionId_160[3]);
                        questionId_160[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("217")) {
                        Integer value = Integer.valueOf(questionId_160[1]);
                        questionId_160[1] = String.valueOf(value + 1);
                    }
                }
                //对服务人员形象满意度？(如是否穿上公司制服)？
                if (customerReport.getQuestionId().equals(161L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("194")) {
                        Integer value = Integer.valueOf(questionId_161[9]);
                        questionId_161[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("200")) {
                        Integer value = Integer.valueOf(questionId_161[7]);
                        questionId_161[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("206")) {
                        Integer value = Integer.valueOf(questionId_161[5]);
                        questionId_161[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("212")) {
                        Integer value = Integer.valueOf(questionId_161[3]);
                        questionId_161[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("218")) {
                        Integer value = Integer.valueOf(questionId_161[1]);
                        questionId_161[1] = String.valueOf(value + 1);
                    }
                }
                //您对这次技术服务人员的工作态度满意度如何？
                if (customerReport.getQuestionId().equals(162L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("195")) {
                        Integer value = Integer.valueOf(questionId_162[9]);
                        questionId_162[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("201")) {
                        Integer value = Integer.valueOf(questionId_162[7]);
                        questionId_162[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("207")) {
                        Integer value = Integer.valueOf(questionId_162[5]);
                        questionId_162[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("213")) {
                        Integer value = Integer.valueOf(questionId_162[3]);
                        questionId_162[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("219")) {
                        Integer value = Integer.valueOf(questionId_162[1]);
                        questionId_162[1] = String.valueOf(value + 1);
                    }
                }
                //这次技术服务支持是否达到您的期望？
                if (customerReport.getQuestionId().equals(163L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("196")) {
                        Integer value = Integer.valueOf(questionId_163[9]);
                        questionId_163[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("202")) {
                        Integer value = Integer.valueOf(questionId_163[7]);
                        questionId_163[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("208")) {
                        Integer value = Integer.valueOf(questionId_163[5]);
                        questionId_163[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("214")) {
                        Integer value = Integer.valueOf(questionId_163[3]);
                        questionId_163[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("220")) {
                        Integer value = Integer.valueOf(questionId_163[1]);
                        questionId_163[1] = String.valueOf(value + 1);
                    }
                }
                //您对这次技术服务工作整体满意度评分如何？
                if (customerReport.getQuestionId().equals(164L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("197")) {
                        Integer value = Integer.valueOf(questionId_164[9]);
                        questionId_164[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("203")) {
                        Integer value = Integer.valueOf(questionId_164[7]);
                        questionId_164[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("209")) {
                        Integer value = Integer.valueOf(questionId_164[5]);
                        questionId_164[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("215")) {
                        Integer value = Integer.valueOf(questionId_164[3]);
                        questionId_164[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("221")) {
                        Integer value = Integer.valueOf(questionId_164[1]);
                        questionId_164[1] = String.valueOf(value + 1);
                    }
                }
                //您对技术服务人员提供的解决方案感到满意吗?
                if (customerReport.getQuestionId().equals(166L)) {
                    //20分
                    if (customerReport.getAnswerId().equals("241")) {
                        Integer value = Integer.valueOf(questionId_166[9]);
                        questionId_166[9] = String.valueOf(value + 1);
                    }
                    //40分
                    if (customerReport.getAnswerId().equals("242")) {
                        Integer value = Integer.valueOf(questionId_166[7]);
                        questionId_166[7] = String.valueOf(value + 1);
                    }
                    //60分
                    if (customerReport.getAnswerId().equals("243")) {
                        Integer value = Integer.valueOf(questionId_166[5]);
                        questionId_166[5] = String.valueOf(value + 1);
                    }
                    //80分
                    if (customerReport.getAnswerId().equals("244")) {
                        Integer value = Integer.valueOf(questionId_166[3]);
                        questionId_166[3] = String.valueOf(value + 1);
                    }
                    //100分
                    if (customerReport.getAnswerId().equals("245")) {
                        Integer value = Integer.valueOf(questionId_166[1]);
                        questionId_166[1] = String.valueOf(value + 1);
                    }
                }
            }
        }
        //百分比赋值
        questionId_159 = countPercent(questionId_159);
        questionId_160 = countPercent(questionId_160);
        questionId_161 = countPercent(questionId_161);
        questionId_162 = countPercent(questionId_162);
        questionId_163 = countPercent(questionId_163);
        questionId_164 = countPercent(questionId_164);
        questionId_166 = countPercent(questionId_166);
        //平均值赋值
        questionId_159 = countAverage(questionId_159);
        questionId_160 = countAverage(questionId_160);
        questionId_161 = countAverage(questionId_161);
        questionId_162 = countAverage(questionId_162);
        questionId_163 = countAverage(questionId_163);
        questionId_164 = countAverage(questionId_164);
        questionId_166 = countAverage(questionId_166);

        scoreList.add(questionId_159);
        scoreList.add(questionId_160);
        scoreList.add(questionId_161);
        scoreList.add(questionId_162);
        scoreList.add(questionId_163);
        scoreList.add(questionId_164);
        scoreList.add(questionId_166);
        return scoreList;
    }

    //计算百分比
    //[0~12] [题目名称:0,100分:1,百分比:2,80分:3,百分比:4,60分:5,百分比:6,40分:7,百分比:8,20分:9,百分比:10,平均分:11]
    private String[] countPercent(String[] arrayStr) {
        double index1 = Double.valueOf(arrayStr[1]);
        double index3 = Double.valueOf(arrayStr[3]);
        double index5 = Double.valueOf(arrayStr[5]);
        double index7 = Double.valueOf(arrayStr[7]);
        double index9 = Double.valueOf(arrayStr[9]);
        //100分
        if (arrayStr[1].equals("0")) {
            arrayStr[2] = "0%";
        } else {
            Double doubleValue = index1 / (index1 + index3 + index5 + index7 + index9) * 100;
            String value = doubleGeTowDecimal(doubleValue) + "%";
            arrayStr[2] = value;
        }
        //80分
        if (arrayStr[3].equals("0")) {
            arrayStr[4] = "0%";
        } else {
            Double doubleValue = index3 / (index1 + index3 + index5 + index7 + index9) * 100;
            String value = doubleGeTowDecimal(doubleValue) + "%";
            arrayStr[4] = value;
        }
        //60分
        if (arrayStr[5].equals("0")) {
            arrayStr[6] = "0%";
        } else {
            Double doubleValue = index5 / (index1 + index3 + index5 + index7 + index9) * 100;
            String value = doubleGeTowDecimal(doubleValue) + "%";
            arrayStr[6] = value;
        }
        //40分
        if (arrayStr[7].equals("0")) {
            arrayStr[8] = "0%";
        } else {
            Double doubleValue = index7 / (index1 + index3 + index5 + index7 + index9) * 100;
            String value = doubleGeTowDecimal(doubleValue) + "%";
            arrayStr[8] = value;
        }
        //20分
        if (arrayStr[9].equals("0")) {
            arrayStr[10] = "0%";
        } else {
            Double doubleValue = index9 / (index1 + index3 + index5 + index7 + index9) * 100;
            String value = doubleGeTowDecimal(doubleValue) + "%";
            arrayStr[10] = value;
        }
        return arrayStr;
    }

    //保留2位小数 并转成String
    private String doubleGeTowDecimal(Double doubleNum) {
        DecimalFormat format = new DecimalFormat("#0.00");
        String doubleStr = format.format(doubleNum);
        return doubleStr;
    }

    //保留2位小数 并转成String
    private String doubleGeTowDecimal_Average(Double doubleNum) {
        DecimalFormat format = new DecimalFormat("#0.00");
        String doubleStr = format.format(doubleNum * 10);
        return doubleStr;
    }

    //计算平均值
    //[0~12] [题目名称:0,100分:1,百分比:2,80分:3,百分比:4,60分:5,百分比:6,40分:7,百分比:8,20分:9,百分比:10,平均分:11]
    private String[] countAverage(String[] arrayStr) {
        arrayStr = countValue(arrayStr);
        return arrayStr;
    }

    //计算平均值
    private String[] countValue(String[] arrayStr) {
        double index1 = Double.valueOf(arrayStr[1]);
        double index3 = Double.valueOf(arrayStr[3]);
        double index5 = Double.valueOf(arrayStr[5]);
        double index7 = Double.valueOf(arrayStr[7]);
        double index9 = Double.valueOf(arrayStr[9]);
        double countDouble = (index1 + index3 * 0.8 + index5 * 0.6 + index7 * 0.4 + index9 * 0.2) * 10;
        if (countDouble == 0) {
            arrayStr[11] = "0";
        } else {
            double countSum = index1 + index3 + index5 + index7 + index9;
            double divideValue = countDouble / countSum;
            String doubleStr = doubleGeTowDecimal_Average(divideValue);
            arrayStr[11] = doubleStr;
        }
        return arrayStr;
    }

}
